package com.example.takeaway.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.example.takeaway.entity.Comments;
import com.example.takeaway.serivce.ICommentsService;
import com.example.takeaway.utils.PageResult;
import com.example.takeaway.utils.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * commentController
 * 
 * @author ruoyi
 * @date 2025-06-02
 */
@RestController
@RequestMapping("/system/comments")
public class CommentsController
{
    @Autowired
    private ICommentsService commentsService;

    /**
     * 查询comment列表
     */
//    @PreAuthorize("@ss.hasPermi('system:comments:list')")
    @GetMapping("/list")
    public ServerResponse list(Comments comments,@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10")int pageSize)
    {
//        startPage();
        PageResult pageResult = commentsService.selectCommentsList(comments,pageNum,pageSize);
        return  ServerResponse.createBysuccess(200,pageResult,"查询成功");
    }

    /**
     * 导出comment列表
     */
//    @PreAuthorize("@ss.hasPermi('system:comments:export')")
//    @Log(title = "comment", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, Comments comments)
//    {
//        List<Comments> list = commentsService.selectCommentsList(comments);
//        ExcelUtil<Comments> util = new ExcelUtil<Comments>(Comments.class);
//        util.exportExcel(response, list, "comment数据");
//    }

    /**
     * 获取comment详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:comments:query')")
    @GetMapping(value = "/{commentsid}")
    public ServerResponse getInfo(@PathVariable("commentsid") Long commentsid)
    {
        Comments comments = commentsService.selectCommentsByCommentsid(commentsid);
        return ServerResponse.createBysuccess(200,comments,"查询成功");
 }

    /**
     * 新增comment
     */
//    @PreAuthorize("@ss.hasPermi('system:comments:add')")
//    @Log(title = "comment", businessType = BusinessType.INSERT)
    @PostMapping
    public ServerResponse add(@RequestBody Comments comments)
    {
        comments.setCommentdate(new Date());
        int i  = commentsService.insertComments(comments);
        if (i>0){
            return  ServerResponse.createBysuccess(200,"","新增成功");
        }else{
            return  ServerResponse.createByfail(400,"新增失败");
        }

    }

    /**
     * 修改comment
     */
//    @PreAuthorize("@ss.hasPermi('system:comments:edit')")
//    @Log(title = "comment", businessType = BusinessType.UPDATE)
    @PutMapping
    public ServerResponse edit(@RequestBody Comments comments)
    {
        comments.setCommentdate(new Date());
        int i = commentsService.updateComments(comments);
        if (i>0){
            return  ServerResponse.createBysuccess(200,"","新增成功");
        }else{
            return  ServerResponse.createByfail(400,"新增失败");
        }
    }

    /**
     * 删除comment
     */
//    @PreAuthorize("@ss.hasPermi('system:comments:remove')")
//    @Log(title = "comment", businessType = BusinessType.DELETE)
	@DeleteMapping("/{commentsids}")
    public ServerResponse remove(@PathVariable Long[] commentsids)
    {
        int i = commentsService.deleteCommentsByCommentsids(commentsids);
        if (i>0){
            return  ServerResponse.createBysuccess(200,"","新增成功");
        }else{
            return  ServerResponse.createByfail(400,"新增失败");
        }
    }
}
